
// eslint-disable-next-line no-shadow
export function search({ pg, search } = {}) {
  return RSocket.rs({
    route: 'users',
    args: {
      pg: { ...pg },
      search: {
        userTypes: 100,
        ...search,
      },
    },
  });
}

// eslint-disable-next-line no-shadow
export function count({ search }) {
  return RSocket.rr({
    route: 'users.count',
    args: {
      search: {
        userTypes: 100,
        ...search,
      },
    },
  });
}

export function get(username) {
  return RSocket.rr({
    route: `users.${username}`,
  });
}

// export function getLastLogin(username) {
//   return RSocket.rr({
//     route: `users.${username}.lastlogin`,
//   });
// }

export function getLastLoginBatch(usernames) {
  return RSocket.rs({
    route: 'users.lastlogin.batch',
    data: usernames,
  });
}

// export function getUserRoles(username) {
//   return RSocket.rs({
//     route: `roles.${username}.belong`,
//   });
// }

export function create(user) {
  return RSocket.rr({ route: 'users.create', data: user });
}

export function update(user) {
  return RSocket.rr({ route: 'users.update', data: user });
}

export function deleteUser(username) {
  return RSocket.rr({ route: `users.${username}.delete` });
}

/**
 * 获取文件二进制内容
 * @param {*} binaryId 文件标识
 * @returns  包文件内容的Buffer数组
 */
export function getFile(binaryId) {
  return RSocket.rs({ route: `files.${Util.escapeRoute(binaryId)}`, binary: true });
}


/**
 * 获取文件元数据
 * @param {*} binaryId 文件标识
 * @returns 文件元数据
 */
export function fileMeta(binaryId) {
  return RSocket.rr({ route: `files.${Util.escapeRoute(binaryId)}.meta` });
}


/**
 * 所属角色
 */
export function getRoles(username) {
  return RSocket.rs({
    route: `roles.${Util.escapeRoute(username)}.belong`,
  });
}

export function getLastLogin(username) {
  return RSocket.rr({
    // eslint-disable-next-line @iceworks/best-practices/recommend-polyfill
    route: `users.${username && username.replaceAll('.', '%2E')}.lastlogin`,
  });
}

/**
 * 判断用户是否存在
 * @param {*} args
 */
export function getIsUser(username) {
  return RSocket.rr({
    route: `users.${username}.exists`,
  });
}

/**
 * 获取指定用户
 * @param {*} args
 */
export function getUserMobile(mobile) {
  return RSocket.rr({
    route: `users.bymobile.${mobile}`,
  });
}

/**
 * 校验用户是否存在
 * @param {*} args
 */
export function getUserProp(args) {
  return RSocket.rr({
    route: 'users.prop.exists',
    args: { ...args },
  });
}

export function getUserCount({ page = 0, size = 10, sort, ...args } = {}) {
  return RSocket.rr({
    route: 'users.count',
    args: {
      pg: { page, size, sort: sort && sort.split(',') },
      search: { ...args },
    },
  });
}

/**
 * 获取用户列表
 * @param {*} args
 */
export function getUsers({ page = 0, size = 10, sort, ...args } = {}) {
  return RSocket.rs({
    route: 'users.search',
    args: {
      pg: { page, size, sort: sort && sort.split(',') },
      search: { ...args },
    },
  });
}
